package com.exm.collectcodenew.juc.aqs;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
//调用信号量的release方法相当于让计数器递增，调用信号量的acquire方法相当于让计数器递减。
public class SemaphoreTest1 {
    //创建一个Semaphore实例
    private static Semaphore semaphore= new Semaphore(0);

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        //将线程A添加到线程池
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+" over");
                semaphore.release();
            }
        });
        //将线程B添加到线程池
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread()+" over");
                semaphore.release();
            }
        });
        //等待子线程执行完毕
        semaphore.acquire(2);
        System.out.println("all child thread over!");
        //关闭线程池
        executorService.shutdown();
    }
}
